// KTraceLog.cpp : implementation file
//

#include "stdafx.h"
#include "KTraceLog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// KTraceLog dialog

IMPLEMENT_DYNCREATE(KTraceLog, CDialog)

KTraceLog::KTraceLog() :CDialog(KTraceLog::IDD)
{
	//{{AFX_DATA_INIT(KTraceLog)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void KTraceLog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(KTraceLog)
	DDX_Control(pDX, IDC_LIST1, m_TraceList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(KTraceLog, CDialog)
	//{{AFX_MSG_MAP(KTraceLog)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// KTraceLog message handlers


/////////////////////////////////////////////////////////////////////////////
// CTracePage message handlers

void KTraceLog::AddTraceLine(int nLevel, LPCTSTR pstrFormat, ...)
{
	CString str;

	// format and write the data we were given
	va_list args;
	va_start(args, pstrFormat);
	str.FormatV(pstrFormat, args);

	try
	{
		// create new message
		CLogMsg *pLogMsg = new CLogMsg;

		GetLocalTime(&pLogMsg->m_sysTime);
		pLogMsg->m_nLevel = nLevel;
		pLogMsg->m_strText = str;

		m_QueueLock.Lock();
		m_LogQueue.AddTail(pLogMsg);
		m_QueueLock.Unlock();
				
		// schedule log action
		PostMessage(WM_ADDTRACELINE);
	}
	catch(...)
	{
	
	}
}


LRESULT KTraceLog::OnAddTraceLine(WPARAM, LPARAM)
{
	CLogMsg *pLogMsg;
	
	try
	{
		// get first message in the queue
//		m_QueueLock.Lock();
		pLogMsg = (CLogMsg *)m_LogQueue.RemoveHead();
//		m_QueueLock.Unlock();

		switch(pLogMsg->m_nLevel)
		{
			case 1:
				m_TraceList.AddString(pLogMsg->m_strText, RGB(0,130,0));
				break;
			case 2:
				m_TraceList.AddString(pLogMsg->m_strText, RGB(0,0,255));
				break;
			case 3:
				m_TraceList.AddString(pLogMsg->m_strText, RGB(255,0,0));
				break;
			default:
				m_TraceList.AddString(pLogMsg->m_strText, RGB(0,0,0));
				break;

		}
/*		CString strDateTime;
		strDateTime.Format("%02d/%02d/%02d %02d:%02d:%02d.%03d ", 
							pLogMsg->m_sysTime.wDay, pLogMsg->m_sysTime.wMonth, pLogMsg->m_sysTime.wYear,
							pLogMsg->m_sysTime.wHour, pLogMsg->m_sysTime.wMinute, pLogMsg->m_sysTime.wSecond, 
							pLogMsg->m_sysTime.wMilliseconds);

		switch(pLogMsg->m_nLevel)
		{
			case 1:
				m_LogFile << error_lvl << strDateTime << pLogMsg->m_strText << endl;
				break;
			case 2:
				m_LogFile << warning_lvl << strDateTime << pLogMsg->m_strText << endl;
				break;
			case 3:
			default:
				m_LogFile << trace_lvl << strDateTime << pLogMsg->m_strText << endl;
				break;
		} */
		delete pLogMsg;
	}
	catch(...)
	{
		// something is wrong...
	}
	return TRUE;
}

void KTraceLog::OnSize(UINT nType, int cx, int cy) 
{

	CDialog::OnSize(nType, cx, cy);
	
	if (IsWindow(::GetDlgItem(m_hWnd, IDC_LIST1)))
	{
		CRect rect;
		GetClientRect(rect);
	//	rect.DeflateRect(5,5,10,10);
		m_TraceList.MoveWindow(rect);
	}	
}
